<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 头部 -->
        <el-form label-width="80px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="活动名称">
                <el-input
                  v-model="keyword"
                  placeholder="根据编号搜索"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="18" style="text-align:right">
              <el-button size="small" @click="clear">清除</el-button>
              <el-button size="small" type="primary" @click="search()">搜索</el-button>
            </el-col>
          </el-row>
        </el-form>
        <!-- 数剧条数警示 -->
        <el-alert :title="'数据一共' + count + '条'" type="info" show-icon :closable="false" style="margin-bottom: 20px;">
        </el-alert>
        <!-- 表单 -->
          <el-table border :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="编号" width="180"></el-table-column>
          <el-table-column label="题型" width="80">
            <template slot-scope="scope">
                {{ scope.row.questionType | questionTypes}}
              </template>
          </el-table-column>
          <el-table-column label="题目编号" >
            <template slot-scope="scope">
              <div class="questionsid" v-for="(item,index) in scope.row.questionIDs" :key="index" @click="dialogVisible = true">{{item.number}}
              </div>

            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间"> </el-table-column>
          <el-table-column prop="totalSeconds " label="答题时间(S)"></el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)"> </el-table-column>
          <el-table-column prop="userName" label="录入人"> </el-table-column>
          <el-table-column prop="btn" label="操作">
            <template slot-scope="scope">
               <el-button @click="del(scope.row)" type="danger" icon="el-icon-delete" circle></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination">
          <el-pagination
            style="margin-left: auto"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="query.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="query.pagesize"
            layout="  prev, pager, next, sizes, jumper"
            :total="count"
          >
          </el-pagination>
        </div>
      </el-card>
      <el-dialog
        title="题目预览"
        :visible.sync="dialogVisible"
        width="50%">
        <!-- 题目预览 -->
        <div>
          <!-- 第一行 -->
         <div>
            <div>
            <el-row>
              <el-col :span="6">【题型】：</el-col>
              <el-col :span="6">【编号】：20</el-col>
              <el-col :span="6">【难度】：困难</el-col>
              <el-col :span="6">【标签】：</el-col>
            </el-row>
          </div>
          <div style="margin-top:30px">
            <el-row>
              <el-col :span="6">【学科】：java</el-col>
              <el-col :span="6">【目录】：javaWEB</el-col>
              <el-col :span="6">【方向】：互联网金融</el-col>
              <el-col :span="6"></el-col>
            </el-row>
          </div>
         </div>
          <hr>
          <!-- 第二行 -->
          <div>
            <div>
             <el-row>
              <el-col :span="6">【题干】：</el-col>
            </el-row>
          </div>
          <div style="margin-top:20px">
             <el-row>
              <el-col :span="6">php是世界上最好的语言吗？</el-col>
            </el-row>
          </div>
          </div>
          <hr>
          <!-- 第三行 -->
          <div style="margin:20px 0">
            <div style="display:flew;align-items:center">
                <span style="margin-right:10px">【参考答案】：</span>
                <el-button type="danger" @click="showvideo=true">视频答案预览</el-button>
            </div>

             <div style="margin-top:10px">
                <video v-if="showvideo"
                height="300"
                controls="controls"
                autoplay="autoplay"
              ></video>
             </div>

          </div>
          <hr>
          <!-- 第四行 -->
          <div style="margin:20px 0">
            <el-row>
              <el-col :span="6">【答案解析】：</el-col>
            </el-row>
          </div>
          <hr>
          <!-- 第五行 -->
          <div style="margin:20px 0">
            <el-row>
              <el-col :span="6">【题目备注】：</el-col>
            </el-row>
          </div>
        </div>
        <span slot="footer" style="display: flex;justify-content: flex-end;">
          <el-button @click="dialogVisible = false">关闭</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
// 导入题型枚举数据
import { questionType } from '@/api/hmmm/constants'
export default {
  name: 'Randoms',
  data () {
    return {
      tableData: [], // 表格数据
      // 分页数据
      query: {
        page: 1, // 页码
        pagesize: 10 // 每页条数
      },
      // 搜索数据
      keyword: '',
      count: 0, // 总条数
      userId: {},
      dialogVisible: false, // 弹框显示隐藏
      showvideo: false // 答案预览显示隐藏

    }
  },
  created () {
    // 调用获取组题列表的方法
    this.getRandoms()
  },
  methods: {
    // 删除
    async del (row) {
      const confirmRes = await this.$confirm('此操作将永久删除该列表, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmRes === 'cancel') {
        return this.$message('您取消了删除')
      }
      // 先判断 tableData 中数组的长度，如果长度等于 1，前面又执行删除，并且删除成功
      // 就需要返回上一页
      if (this.tableData.length === 1) {
        // 页码 -1
        this.query.page--

        // 在此判断当前是删除的是不是第一页的最后一条数据，如果是我们需要将页码重置为 1
        if (this.query.page < 1) {
          this.query.page = 1
        }
      }

      // console.log(id)
      await removeRandoms(row)
      this.getRandoms()
    },
    // 清除
    clear () {
      this.keyword = ''
      this.query.keyword = ''
      this.getRandoms()
    },
    // 搜索
    async search () {
      this.query.page = 1
      this.query.keyword = this.keyword
      // console.log(this.query)
      await randoms(this.query)
      this.getRandoms()
    },
    // 获取组题列表
    async getRandoms () {
    // 调用获取员工列表的 API
      const res = await randoms(this.query)
      console.log(res)
      // 将返回的结果赋值 data 中的数据
      this.tableData = res.data.items
      this.count = res.data.counts
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange (val) {
      this.query.pagesize = val
      this.query.page = 1
      this.getRandoms()
    },

    // 当前页面发生改变时触发
    handleCurrentChange (page) {
      this.query.page = page
      this.getRandoms()
    }
  },
  filters: {
    // 题型的枚举
    questionTypes (val) {
      // console.log(val)
      const res = questionType.find(item => item.value === Number(val))

      // console.log(res)
      return res.label
    }
  }
}
</script>

<style scoped>
.inp_left {
  width: 30%;
}
.pagination {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}
.questionsid {
  color: rgb(0, 153, 255);
}
</style>
